#ifndef AXISTILEDMAPSCENE_H
#define AXISTILEDMAPSCENE_H

#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QPolygonF>
#include <QPointF>
#include <QMap>
#include <QList>
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include "axistiledmap.hpp"

class AxisSprite;

class AxisTiledMapScene : public QGraphicsScene
{
public:
    AxisTiledMapScene(QObject* parent = 0);
    void addSprite(AxisSprite* sp);

    void setMousePos(QPoint pos);
    void setSize(int w, int h);
    QPolygonF calPolygonPath(int x, int y);

    AxisTiledMapBase* getMap(){ return &_map; }
    void drawMousePos(QPainter* painter, int w, int h);
    void drawGrid(QPainter* painter, const QRectF& rect, QColor gridColor) const;
    void drawGridSelection(QPainter* painter,
                           const QRegion& region,
                           const QColor& color,
                           const QRectF& exposed) const;

    QPointF screenToTileCoords(qreal x, qreal y) const;
    QPointF screenToTileCoords(QPointF p) const;
    QPointF tileToScreenCoords(qreal x, qreal y) const;
    QPointF tileToScreenCoords(QPointF p) const;
    QPointF pixelToScreenCoords(qreal x, qreal y) const;
    QPolygonF tileRectToScreenPolygon(const QRect& rect) const;

protected:
    void drawBackground(QPainter* painter, const QRectF &rect);
    void drawForeground(QPainter* painter, const QRectF &rect);

    void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
    void mousePressEvent(QGraphicsSceneMouseEvent* event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);

protected:
    QList<AxisSprite*> _sprites;
    QList<QPoint> _path;
    AxisSprite* _selectsprite;
    AxisTiledMapBase _map;
    QPointF _lastmousepos;
    int _tilex = 0;
    int _tiley = 0;
    int _width = 0;
    int _height = 0;
};

#endif // AXISTILEDMAPSCENE_H
